package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;
import com.collect.community.R;
import com.csoa.community.net.HttpAsyncTask;
import com.csoa.community.requests.AbstractRequest;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.AndroidUtils;
import com.csoa.community.utils.ConvertUtils;
import com.csoa.community.utils.OverrideGestureListener;
import com.csoa.community.utils.OverrideGestureListener.OnPageProcessListener;
import com.csoa.community.widget.NarView;

@SuppressLint("NewApi")
@TargetApi(8)
public class BaseActivity extends Activity implements OnTouchListener{

	protected TextView mCommonButton;
	protected ImageView mCommonLogo;
	protected TextView mCommonTitle;
	public static BaseActivity _activity;
	protected Handler mHandler = new Handler();
	private ProgressDialog progressDialog;
	public static List<BaseActivity> activities = null;
	// 百度MapAPI的管理类
	public BMapManager mBMapMan = null;
	public String mStrKey = "617FC31879D763BCA22F1F514A213D77C69AB5A6";
	boolean m_bKeyRight = true; // 授权Key正确，验证通过
	LocationListener mLocationListener = null;// create时注册此listener，Destroy时需要Remove
	private NarView narView;
	private OnPageProcessListener onPageProcessListener;
	private GestureDetector mGestureDetector;

	public void hideCommonTitle() {
		if ((mCommonTitle != null) && (mCommonTitle.getVisibility() == 0))
			mCommonTitle.setVisibility(4);
	}

	protected void hideInputMethod(View paramView) {
		if (paramView != null) {
			((InputMethodManager) getSystemService("input_method"))
					.hideSoftInputFromWindow(paramView.getWindowToken(), 2);
		}
		getWindow().setSoftInputMode(3);
	}

	public void initData() {
	}

	public void initUI() {
	}

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		_activity = this;
		if (this instanceof MainActivity) {
			if (activities == null) {
				activities = new ArrayList<BaseActivity>();
			}
			activities.add(this);
		}

		initUI();
		initData();
		hideInputMethod(null);
		onPageProcessListener = new OnPageProcessListener() {

			@Override
			public void onPrePage() {
				ProcessPrePage();
			}

			@Override
			public void onNextPage() {
				ProcessNextPage();
			}
		};
	}
	

	protected void ProcessNextPage() {

	}

	protected void ProcessPrePage() {

	}

	public void hiddenNar(int id) {
		narView = (NarView) this.findViewById(R.id.comNarView);
		narView.findViewById(id).setVisibility(View.GONE);
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		if ((paramInt == 4) && (paramKeyEvent.getRepeatCount() == 0)) {
			finish();
			super.onKeyDown(paramInt, paramKeyEvent);
		}
		return false;
	}

	protected void onPause() {
		super.onPause();
	}

	protected void onResume() {
		super.onResume();
		adjustScreenBright();
		this.getWindow();

	}

	public void adjustScreenBright() {
	}

	public void showCommonButton(int paramInt,
			View.OnClickListener paramOnClickListener) {
	}

	public void showCommonButton(String paramString,
			View.OnClickListener paramOnClickListener) {
	}

	public void setDataForView(AbstractRequest request) {

	};

	protected void request(AbstractRequest request) {
		if (request != null) {
			new HttpAsyncTask(_activity, new OnUICallback() {

				@Override
				public void onProgress(int progress, String msg) {

				}

				@Override
				public void onGetResult(AbstractRequest request) {
					setDataForView(request);
				}

				@Override
				public void onCancel() {

				}
			});
			//
		}
	}

	public void showCommonLogo() {
		if (mCommonLogo == null)
			mCommonLogo = ((ImageView) findViewById(2131296331));
		if ((mCommonLogo != null) && (mCommonLogo.getVisibility() != 0))
			mCommonLogo.setVisibility(0);
	}

	public void showCommonTitle(String paramString) {
		if ((mCommonTitle != null) && (mCommonTitle.getVisibility() != 0)) {
			mCommonTitle.setVisibility(0);
		}
	}

	public void showToast(final String content) {
		Toast localToast = Toast.makeText(BaseActivity.this, content,
				Toast.LENGTH_LONG);
		localToast.show();
	}

	public void showProgress() {
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(this);
		}
		if (!progressDialog.isShowing()) {
			progressDialog.setMessage("正在加载数据,请稍后..");
			progressDialog.setCancelable(true);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.show();
		}
	}

	public void dismisProgress() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	public interface OnUICallback {
		public void onGetResult(AbstractRequest result);

		public void onCancel();

		void onProgress(int progress, String msg);
	}

	public void closeAllActivity() {
		if (activities != null) {
			for (int i = 0; i < activities.size(); i++) {
				activities.get(i).finish();
			}
		}
	}

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	public static class MyGeneralListener implements MKGeneralListener {
		@Override
		public void onGetNetworkState(int iError) {
			Log.d("MyGeneralListener", "onGetNetworkState error is " + iError);
			setGlobalLocate(null);
			Toast.makeText(_activity, "网络错误,获取定位信息失败！", Toast.LENGTH_LONG)
					.show();

		}

		@Override
		public void onGetPermissionState(int iError) {
			Log.d("MyGeneralListener", "onGetPermissionState error is "
					+ iError);
			if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
				// 授权Key错误：
				Toast.makeText(_activity, "请在BMapApiDemoApp.java文件输入正确的授权Key！",
						Toast.LENGTH_LONG).show();
				_activity.m_bKeyRight = false;
			}
		}
	}

	/**
	 * 启动地图定位服务，获取当前经纬度
	 */
	public void initMapLocation() {
		if (mBMapMan == null) {
			mBMapMan = new BMapManager(this);
			mBMapMan.init(this.mStrKey, new MyGeneralListener());
			mBMapMan.getLocationManager().setNotifyInternal(10, 5);
		}

		// 注册定位事件
		mLocationListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location location) {
				setGlobalLocate(location);
				double altitude = location.getAltitude();
				double latitude = location.getLatitude();
				double longitude = location.getLongitude();
				Log.i("msg", "" + latitude + "" + longitude + "" + altitude);
				setLocateInfo();
			}
		};
	}

	public static void setGlobalLocate(Location location) {
		if (location != null) {
			GlobalVars.getInst().latitude = ConvertUtils.doubleScale(
					location.getLatitude(), 6);
			GlobalVars.getInst().longitude = ConvertUtils.doubleScale(
					location.getLongitude(), 6);
			boolean isHas = location.hasAltitude();
			if (isHas) {
				GlobalVars.getInst().altitude = ConvertUtils.doubleScale(
						location.getAltitude(), 6);
			}
		} else {
			GlobalVars.getInst().latitude = 0;
			GlobalVars.getInst().longitude = 0;
			GlobalVars.getInst().altitude = 0;
		}

	}

	public void setLocateInfo() {

	}

	/**
	 * 开始定位，用在activity的onResume()方法中
	 */
	public void startLocation() {
		if (AndroidUtils.isNetworkValidate(_activity)) {
			if (mBMapMan != null) {
				mBMapMan.getLocationManager().requestLocationUpdates(
						mLocationListener);
				mBMapMan.start();
			}
		} else {
			setGlobalLocate(null);
			showToast("没有网络不能使用GPS定位功能");
		}

	}

	//
	/**
	 * 取消定位，用在activity的onPause()方法中
	 */
	public void stopLocation() {
		// 移除listener
		if (mBMapMan != null) {
			mBMapMan.getLocationManager().removeUpdates(mLocationListener);
			mBMapMan.stop();
		}
	}

	public void destroyBMapManager() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
	}





	@Override
	protected void onDestroy() {

		super.onDestroy();
		dismisProgress();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mGestureDetector == null) {
			mGestureDetector = new GestureDetector(new OverrideGestureListener(
					this, onPageProcessListener));
		}
		return mGestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (mGestureDetector == null) {
			mGestureDetector = new GestureDetector(new OverrideGestureListener(
					this, onPageProcessListener));
		}
		return mGestureDetector.onTouchEvent(event);
	}
	
	
	



}
